package com.bsi.login.controller;


import com.bsi.core.exception.ResponseEntity;
import com.bsi.core.exception.SystemCode;
import com.bsi.login.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 企业登录注册相关
 *
 * @author system
 * @date 2019/12/26
 */
@RestController
@RequestMapping("/login")
@Api(value = "企业登录注册", tags = {"企业登录注册"})
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @Autowired
    protected RedisTemplate redisTemplate;

    @PostMapping("/in")
    @ApiOperation(value = "企业登录", notes = "企业登录", tags = {"登录"})
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "loginName", value = "企业登录名"),
            @ApiImplicitParam(required = true, name = "password", value = "企业登录密码"),
    })
    public ResponseEntity login(@RequestParam("loginName") String loginName, @RequestParam("password") String password) {

        if(StringUtils.isBlank(loginName)||StringUtils.isBlank(password)){
            return ResponseEntity.createError(SystemCode.ParamError,"用户名或密码不能为空");
        }
        //根据登录名称查询企业用户
        String userId="登陆成功后获得的用户id";
        //登录成功
        //调用oauth2来生成令牌
        Map loginMsg = loginService.login(loginName,password, userId);
        //用户信息
        redisTemplate.opsForValue().set(userId,new Object());
        return ResponseEntity.createSuccess(loginMsg);
    }

    @PostMapping("/refresh")
    public String refresh(@RequestParam("refresh_token") String refresh_token) {
        // 调用oauth2来生成令牌
        return loginService.refreshToken(refresh_token);
    }

    @PostMapping("check")
    public String check(@RequestParam("token") String token) {
        return loginService.checkToken(token);
    }

    @PostMapping("exit")
    public String exit(@RequestParam("token") String token) {
        return loginService.exit(token);
    }

}
